﻿using System;
using System.IO;
using Microsoft.Win32;
using RiftAddonStudio.Services;

namespace RiftAddonStudio.Project.Services
{
    public class RiftLocatorService : IRiftLocatorService
    {
        private const string riftPathSubKeyName = @""; // Havent found a registry entry for rift yet
        private const string riftPathName = "InstallPath";

        private readonly ProjectPackage riftProjectPackage;

        /// <summary>
        /// Initializes a new instance of the <see cref="RiftLocatorService"/> class.
        /// </summary>
        /// <param name="riftProjectPackage">The rift project package.</param>
        public RiftLocatorService(ProjectPackage riftProjectPackage)
        {
            if (riftProjectPackage == null)
                throw new ArgumentNullException("riftProjectPackage");

            this.riftProjectPackage = riftProjectPackage;
        }

        /// <summary>
        /// Gets a value indicting whether a Rift installation was detected.
        /// </summary>
        /// <value></value>
        public bool IsRiftInstalled
        {
            get
            {
                return this.GetRiftBasePath() != null;
            }
        }

        /// <summary>
        /// Gets the base path to the Rift installation.
        /// </summary>
        /// <returns>The base path to Rift.</returns>
        public string GetRiftBasePath()
        {
            /*
                        var settingsService = riftProjectPackage.GetService<IAddonStudioSettings>();
                        if (settingsService != null && !String.IsNullOrEmpty(settingsService.RiftBasePath) && Directory.Exists(settingsService.RiftBasePath))
                            return settingsService.WowBasePath;
            */

            using (RegistryKey key = Registry.LocalMachine.OpenSubKey(riftPathSubKeyName))
            {
                if (key != null)
                    return (string)key.GetValue(riftPathName, null);
            }

            return null;
        }

        /// <summary>
        /// Gets the latest Rift Interface version.
        /// </summary>
        /// <returns>The latest Rift interface version.</returns>
        //public string GetRiftInterface()
        //{
        //    return "30000";
        //}
    }
}
